package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.MovieAdapter;
import flc.ast.databinding.ActivityMovieBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

public class MovieActivity extends BaseAc<ActivityMovieBinding> {

    public static int type;
    private String url;
    private MovieAdapter movieAdapter;
    private int page = 1;
    private int refreshTime = 200;

    @Override
    protected int onCreate() {
        return R.layout.activity_movie;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivMovieBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rvMovieList.setLayoutManager(new GridLayoutManager(mContext, 3));
        movieAdapter = new MovieAdapter();
        mDataBinding.rvMovieList.setAdapter(movieAdapter);
        movieAdapter.addChildClickViewIds(R.id.ivMovieItemZhui);
        movieAdapter.setOnItemClickListener(this);
        movieAdapter.setOnItemChildClickListener(this);

        mDataBinding.refreshMovieItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshMovieItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshMovieItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData();
                mDataBinding.refreshMovieItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getData();
                mDataBinding.refreshMovieItem.finishRefresh(refreshTime);
            }
        });
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        List<StkResBean> listSp = AppSpUtils.getMovieList();
        if (listSp == null) listSp = new ArrayList<>();
        switch (view.getId()) {
            case R.id.ivMovieItemZhui://追剧
                if (movieAdapter.getItem(position).isSelected()) {
                    for (StkResBean listDatum : listSp) {
                        if (listDatum.getUrl().equals(movieAdapter.getItem(position).getUrl())) {
                            listSp.remove(listDatum);
                            break;
                        }
                    }
                } else {
                    listSp.add(0, movieAdapter.getItem(position));
                }
                AppSpUtils.saveMovieList(listSp);
                getSpData();
                movieAdapter.notifyDataSetChanged();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        MovieDetailActivity.stkResBean = bean;
        startActivity(MovieDetailActivity.class);
    }

    @Override
    protected void initData() {
        if (type == 0) {
            mDataBinding.tvMovieTitle.setText(getString(R.string.latest_recommend));
            url = AppConfig.LATEST_RECOMMEND_DATA;
        } else {
            mDataBinding.tvMovieTitle.setText(getString(R.string.classic_movie));
            url = AppConfig.CLASSIC_DATA;
        }
        getData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getSpData();
        movieAdapter.notifyDataSetChanged();
    }

    private void getSpData() {
        List<StkResBean> listSp = AppSpUtils.getMovieList();
        movieAdapter.setListSp(listSp);
    }

    private void getData() {
        StkResApi.getTagResourceList(this, url, StkResApi.createParamMap(0, 15), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            movieAdapter.setList(result);
                        } else {
                            movieAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

}